//
//  LTHeader.swift
//  myMoya
//
//  Created by xiaoyi on 2020/4/26.
//  Copyright © 2020 lete. All rights reserved.
//
import Foundation
import UIKit
import SnapKit


//MARK: - 设备信息
/// 当前语言是否是中文 ture or false
let kLanguage_is_chinese_xm = NSLocale.preferredLanguages[0].hasPrefix("zh")
// Locale String [Upper Case]
// 英文-香港    EN_HK
// 中文-香港    ZH_HK
// 英文-中国    EN_CN
// 英文-台湾    EN_TW
// 英文-澳门    EN_MO
let kLocal_string_upper_case_xm = NSLocale.current.identifier.uppercased()
// 在非中国的海外地区 - 是否是： 非大陆地区
let kArea_out_China_neidi_xm = (!kLocal_string_upper_case_xm.hasSuffix("CN") || kLocal_string_upper_case_xm.hasSuffix("HK") || kLocal_string_upper_case_xm.hasSuffix("TW") || kLocal_string_upper_case_xm.hasSuffix("MO"))

/// 当前版本号： 例如：2.6.1
let kCurrentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
/// 当前的 CFBundleIdentifier
let kCFBundleIdentifier = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String


var isIphoneX: Bool {
    return UI_USER_INTERFACE_IDIOM() == .phone
        && (max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) == 812
            || max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) == 896)
}



//MARK: - 控制器
//MARK: 顶层控制器
var topVC: UIViewController? {
    var resultVC: UIViewController?
    resultVC = _topVC(UIApplication.shared.keyWindow?.rootViewController)
    while resultVC?.presentedViewController != nil {
        resultVC = _topVC(resultVC?.presentedViewController)
    }
    return resultVC
}

private  func _topVC(_ vc: UIViewController?) -> UIViewController? {
    if vc is UINavigationController {
        return _topVC((vc as? UINavigationController)?.topViewController)
    } else if vc is UITabBarController {
        return _topVC((vc as? UITabBarController)?.selectedViewController)
    } else {
        return vc
    }
}
//MARK: UICollectionView
extension UICollectionView {
    
    func reloadData(animation: Bool = true) {
        if animation {
            reloadData()
        } else {
            UIView .performWithoutAnimation {
                reloadData()
            }
        }
    }
}

//
////MARK: Kingfisher
//extension Kingfisher where Base: ImageView {
//    @discardableResult
//    public func setImage(urlString: String?, placeholder: Placeholder? = UIImage(named: "normal_placeholder_h")) -> RetrieveImageTask {
//        return setImage(with: URL(string: urlString ?? ""),
//                        placeholder: placeholder,
//                        options:[.transition(.fade(0.5))])
//    }
//}
//
//extension Kingfisher where Base: UIButton {
//    @discardableResult
//    public func setImage(urlString: String?, for state: UIControl.State, placeholder: UIImage? = UIImage(named: "normal_placeholder_h")) -> RetrieveImageTask {
//        return setImage(with: URL(string: urlString ?? ""),
//                        for: state,
//                        placeholder: placeholder,
//                        options: [.transition(.fade(0.5))])
//
//    }
//}
//MARK: - SnapKit
extension ConstraintView {
    
    var usnp: ConstraintBasicAttributesDSL {
        if #available(iOS 11.0, *) {
            return self.safeAreaLayoutGuide.snp
        } else {
            return self.snp
        }
    }
}


